//
//  CubicLineChartViewController.m
//  cocoapodText
//
//  Created by apple on 16/8/8.
//  Copyright © 2016年 apple. All rights reserved.
//
#import "ShiPei.h"
//导入时需要手动打上，系统没有提示
#import "Charts-Swift.h"
#import <Foundation/Foundation.h>
#import "CubicLineChartViewController.h"
#define ITEM_COUNT 12

@interface CubicLineSampleFillFormatter : NSObject <ChartFillFormatter>
{
}
@end

@implementation CubicLineSampleFillFormatter

- (CGFloat)getFillLinePositionWithDataSet:(LineChartDataSet *)dataSet dataProvider:(id<LineChartDataProvider>)dataProvider
{
    return -10.f;
}

@end

@interface CubicLineChartViewController ()
@end

@implementation CubicLineChartViewController

LineChartView *sampleCubicLineChartView;

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    [self initChartView];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}

-(void)initChartView{
    sampleCubicLineChartView=[[LineChartView alloc] initWithFrame:CGRectMake(kScreenW/4, kScreenH/4, kScreenW/2, kScreenH/2)];
    sampleCubicLineChartView.backgroundColor = [UIColor whiteColor];
    sampleCubicLineChartView.delegate = self;
    [self.view addSubview:sampleCubicLineChartView];
    [self initChartStyleAndData];
}


-(void)initChartStyleAndData{
    [self setupChartView:sampleCubicLineChartView];
    [self setChartParam:sampleCubicLineChartView];
    [self setLegendStyle:sampleCubicLineChartView];
    [self initChartData];
    [sampleCubicLineChartView animateWithXAxisDuration:2.0 yAxisDuration:2.0];
}

- (void)setupChartView:(LineChartView *)chartView
{
    [chartView setViewPortOffsetsWithLeft:0.f top:20.f right:0.f bottom:0.f];
    chartView.backgroundColor = [UIColor colorWithRed:104/255.f green:241/255.f blue:175/255.f alpha:1.f];
    
    chartView.descriptionText = @"";
    chartView.noDataTextDescription = @"You need to provide data for the chart.";
    
    chartView.dragEnabled = YES;
    [chartView setScaleEnabled:YES];
    chartView.pinchZoomEnabled = NO;
    chartView.drawGridBackgroundEnabled = NO;
}

-(void)setChartParam:(LineChartView *)chartView{
    chartView.xAxis.enabled = NO;
    
    ChartYAxis *yAxis = chartView.leftAxis;
    yAxis.labelFont = [UIFont fontWithName:@"HelveticaNeue-Light" size:12.f];
    [yAxis setLabelCount:6 force:NO];
    yAxis.labelTextColor = UIColor.whiteColor;
    yAxis.labelPosition = YAxisLabelPositionInsideChart;
    yAxis.drawGridLinesEnabled = NO;
    yAxis.axisLineColor = UIColor.whiteColor;
    
    chartView.rightAxis.enabled = NO;
}

//设置图例样式
-(void)setLegendStyle:(LineChartView *)chartView{
    chartView.legend.enabled = NO;
}

- (void)initChartData
{
    [self setDataCount:44 range:100 chart:sampleCubicLineChartView];
}

- (void)setDataCount:(int)count range:(double)range chart:(LineChartView *)chartView
{
    NSMutableArray *xVals = [[NSMutableArray alloc] init];
    
    for (int i = 0; i < count; i++)
    {
        [xVals addObject:[@(i + 1990) stringValue]];
    }
    
    NSMutableArray *yVals1 = [[NSMutableArray alloc] init];
    
    for (int i = 0; i < count; i++)
    {
        double mult = (range + 1);
        double val = (double) (arc4random_uniform(mult)) + 20;
        [yVals1 addObject:[[ChartDataEntry alloc] initWithValue:val xIndex:i]];
    }
    
    LineChartDataSet *set1 = nil;
    if (chartView.data.dataSetCount > 0)
    {
        set1 = (LineChartDataSet *)chartView.data.dataSets[0];
        set1.yVals = yVals1;
        chartView.data.xValsObjc = xVals;
        [chartView.data notifyDataChanged];
        [chartView notifyDataSetChanged];
    }
    else
    {
        set1 = [[LineChartDataSet alloc] initWithYVals:yVals1 label:@"DataSet 1"];
        set1.drawCubicEnabled = YES;
        set1.cubicIntensity = 0.2;
        set1.drawCirclesEnabled = NO;
        set1.lineWidth = 1.8;
        set1.circleRadius = 4.0;
        [set1 setCircleColor:UIColor.whiteColor];
        set1.highlightColor = [UIColor colorWithRed:244/255.f green:117/255.f blue:117/255.f alpha:1.f];
        [set1 setColor:UIColor.whiteColor];
        set1.fillColor = UIColor.whiteColor;
        set1.fillAlpha = 1.f;
        set1.drawHorizontalHighlightIndicatorEnabled = NO;
        set1.fillFormatter = [[CubicLineSampleFillFormatter alloc] init];
        
        LineChartData *data = [[LineChartData alloc] initWithXVals:xVals dataSet:set1];
        [data setValueFont:[UIFont fontWithName:@"HelveticaNeue-Light" size:9.f]];
        [data setDrawValues:NO];
        
        chartView.data = data;
    }
}


#pragma mark - ChartViewDelegate

- (void)chartValueSelected:(ChartViewBase * __nonnull)chartView entry:(ChartDataEntry * __nonnull)entry dataSetIndex:(NSInteger)dataSetIndex highlight:(ChartHighlight * __nonnull)highlight
{
    NSLog(@"chartValueSelected");
}

- (void)chartValueNothingSelected:(ChartViewBase * __nonnull)chartView
{
    NSLog(@"chartValueNothingSelected");
}


@end